<template>
    <div>
        <div class="title">
            <el-input style="width:200px;" placeholder="搜索关键字" v-model="keyWord"></el-input>

            <el-button @click="searchData" type="primary" style="margin-left:20px;">搜索</el-button>
            <el-button @click="addHandler" type="primary" style="margin-left:20px;">新增</el-button>
        </div>
        <div class="data">
            <el-table v-loading="loading" :data="tableData" stripe style="width: 100%">
                <el-table-column type="index" label="序号" align="center" min-width="50"></el-table-column>
                <!--<el-table-column prop="plotCode" label="小区编号" align="center" min-width="70"></el-table-column>-->
                <el-table-column prop="plotName" label="小区名称" align="center" min-width="70"></el-table-column>
                <el-table-column prop="address" label="地址 " align="center" min-width="50"></el-table-column>

                <el-table-column prop="regionName" label="所属地区名称 " align="center" min-width="80"></el-table-column>
                <!--        <el-table-column prop="userId" label="代理商id " align="center" min-width="40"></el-table-column>-->
                <!--        <el-table-column prop="userName" label="代理商 " align="center" min-width="50"></el-table-column>-->
                <el-table-column prop="lng" label="纬度 " align="center" min-width="100"></el-table-column>
                <el-table-column prop="lat" label="经度 " align="center" min-width="100"></el-table-column>

                <el-table-column prop="createTime" label="创建时间 " align="center" min-width="100"></el-table-column>
                <el-table-column label="操作" align="center" min-width="100">
                    <template slot-scope="scope">
                        <el-link @click="editCardHandler(scope.row)" icon="el-icon-edit">编辑</el-link>
                        <!-- <el-link @click="editmenuHandler(scope.row)">配置角色</el-link> -->
                        <i style="cursor: pointer;" @click="deteteHandlet(scope.row)" class="el-icon-delete"></i>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="page"
                    :page-size="pageSize"
                    :total="total"
            ></el-pagination>
        </div>
        <el-dialog :title="popupModalTitle" :visible.sync="dialogVisible" width="50%">
            <el-form ref="form" :model="formData" label-width="120px" :rules="formRule">
                <el-form-item class="nav-form-item" label="小区名称" prop="plotName">
                    <el-input
                            maxlength="15"
                            v-model="formData.plotName"
                            style="width:300px;"
                            placeholder="请输入小区名称"
                            clearable
                    />
                </el-form-item>
                <el-form-item class="nav-form-item" label="小区编号" prop="plotName">
                    <el-input maxlength="15" v-model="formData.plotCode" style="width:300px;" placeholder="请输入小区编号"/>
                </el-form-item>
                <el-form-item class="nav-form-item" label="所属地区代码" prop="plotName">
                    <el-select
                            style="width:120px"
                            v-model="province"
                            @change="selProvince"
                            placeholder="请选择省份"
                    >
                        <el-option
                                v-for="item in provinceList"
                                :label="item.name"
                                :value="item.id"
                                :key="item.id"
                        ></el-option>
                    </el-select>
                    <el-select @change="selCity" style="width:120px" v-model="city" placeholder="请选择市">
                        <el-option v-for="item in cityList" :label="item.name" :value="item.id"
                                   :key="item.id"></el-option>
                    </el-select>
                    <el-select style="width:120px" v-model="formData.regionCode" placeholder="请选择区">
                        <el-option v-for="item in areaList" :label="item.name" :value="item.id"
                                   :key="item.id"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item class="nav-form-item" label="地址" prop="plotName">
                    <el-input
                            maxlength="30"
                            v-model="formData.address"
                            auto-complete="false"
                            style="width:300px;"
                            placeholder="请输入地址"
                    />
                </el-form-item>

                <el-form-item class="nav-form-item" label="经度" prop="lng">
                    <el-input v-model="formData.lng" style="width:300px;" placeholder="请输入经度"/>
                </el-form-item>
                <el-form-item class="nav-form-item" label="纬度" prop="lat">
                    <el-input v-model="formData.lat" style="width:300px;" placeholder="请输入纬度"/>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">取 消</el-button>
        <el-button :disabled="isDisable" type="primary" @click="confimHandler">确 定</el-button>
      </span>
        </el-dialog>
    </div>
</template>
<script>
    import {getPlotList, delPlot, addPlot, editPlot} from '@/api/plot'
    import {getOperatorSelect} from '@/api/operator'

    import {getProvinceList, getCityList, getPoltList} from '@/api/common'
    import {getToken} from '@/utils/auth'

    export default {
        data() {
            return {
                areaList: '',
                city: '',
                cityList: [],
                province: '',
                provinceList: [],
                keyWord: '',
                isDisable: false,
                dataStatus: null,
                showStatus: null,
                loading: false,
                dialogVisible: false,
                operatorId: '',
                date: '',
                id: '',
                isAdd: true,
                search: '',
                tableData: [],
                page: 1,
                pageSize: 10,
                formData: {
                    address: '',
                    lat: '',
                    lng: '',
                    plotCode: '',
                    plotName: '',
                    regionCode: '',
                    userId: ''
                },
                total: 0,

                options: [],
                formRule: {
                    plotName: [{required: true, message: '请输入必填项', trigger: 'blur'}],
                    lng: [{required: true, message: '请输入经度', trigger: 'blur'}],
                    lat: [{required: true, message: '请输入纬度', trigger: 'blur'}]
                },
                popupModalTitle: "新增"
            }
        },
        created() {
            this.getDataList()
            this.getOperatorSelect()
            this.getProvinceList()
        },
        methods: {
            remoteMethod(query) {
                if (query !== '') {
                    console.log(query)
                    this.loading = true
                    // this.options = this.list.filter(item => {
                    //   return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1
                    // })
                } else {
                    this.options = []
                }
            },
            selProvince(val) {
                let param = {
                    pid: val
                }
                getCityList(param).then(res => {
                    this.cityList = [...res.data]
                    this.city = ''
                    this.formData.regionCode = ''
                })
            },
            getProvinceList() {
                getProvinceList().then(res => {
                    this.provinceList = [...res.data]
                })
            },
            selCity(val) {
                let param = {
                    pid: val
                }
                getCityList(param).then(res => {
                    this.areaList = [...res.data]
                    this.formData.regionCode = ''
                })
            },
            getOperatorSelect() {
                getOperatorSelect().then(res => {
                    if (res.data && res.data.length > 0) {
                        this.options = [...res.data]
                        this.operatorId = res.data[0].operatorId
                    }
                })
            },
            searchData() {
                this.page = 1
                this.getDataList()
            },
            getDataList() {
                this.loading = true
                let param = {
                    keyWord: this.keyWord,
                    pageNo: this.page,
                    pageSize: this.pageSize
                }
                getPlotList(param).then(res => {
                    if (res.data && res.data.records.length >= 0) {
                        this.tableData = [...res.data.records]
                        this.total = res.data.total
                    }

                    this.loading = false
                })
            },
            handleCurrentChange(val) {
                this.page = val
                this.getDataList()
            },
            addHandler() {
                this.isAdd = true
                this.formData.address = ''
                this.formData.plotCode = ''
                this.formData.plotName = ''
                this.formData.regionCode = ''
                this.formData.userId = ''
                this.popupModalTitle = '新增'
                this.province = ''
                this.city = ''
                this.formData.regionCode = ''
                this.dialogVisible = true
            },
            confimHandler() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        if (this.isAdd) {
                            this.addCard()
                        } else {
                            this.editCard()
                        }
                    }
                })
            },
            deleteHandler(row) {
                this.$confirm('确定删除所选项吗?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let param = {
                        id: row.id
                    }
                    delPromotion(param).then(res => {
                        if (res.message.code == 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            })
                            this.getDataList()
                        } else {
                            this.$message.error(res.message.message)
                        }
                    })
                })
            },
            initSelect(province, city, region) {
                var that = this
                let param = {
                    pid: province
                }
                getCityList(param).then(res => {
                    this.cityList = [...res.data]
                    let param = {
                        pid: city
                    }
                    getCityList(param).then(res => {
                        this.areaList = [...res.data]
                    })
                })
            },
            editCardHandler(row) {
                this.initSelect(row.provinceCode, row.cityCode, row.regionCode)
                this.isAdd = false
                this.formData.id = row.id
                this.formData.address = row.address
                this.formData.lat = row.lat
                this.formData.lng = row.lng
                this.formData.plotCode = row.plotCode
                this.formData.plotName = row.plotName
                this.formData.regionCode = row.regionCode
                this.formData.userId = row.userId
                this.popupModalTitle = '编辑'
                this.province = row.province
                this.city = row.city
                this.formData.regionCode = row.regionCode
                this.dialogVisible = true
            },
            editCard() {
                this.isDisable = true
                editPlot(this.formData).then(res => {
                    if (res.message.code == 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        })
                        this.isDisable = false
                        this.dialogVisible = false
                        this.getDataList()
                    } else {
                        this.$message.error(res.data)
                    }
                })
            },
            addCard() {
                this.isDisable = true
                addPlot(this.formData).then(res => {
                    if (res.message.code == -1) {
                        this.$message.error(res.data)
                    } else {
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        })
                        this.isDisable = false
                        this.dialogVisible = false
                        this.getDataList()
                    }
                })
            },
            getUseRecodsHandler(row) {
                this.$router.push({
                    path: 'card/useRecods',
                    query: {
                        cardNo: row.cardNo
                    }
                })
            },
            deteteHandlet(row) {
                this.$confirm('确定删除所选项吗?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let para = {
                        id: row.id
                    }
                    delPlot(para).then(res => {
                        if (res.message.code == 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            })
                            this.isDisable = false
                            this.dialogVisible = false
                            this.getDataList()
                        } else {
                            this.$message.error(res.data)
                        }
                    })
                })
            }
        }
    }
</script>
<style scoped>
    .title {
        margin-left: 20px;
        margin-top: 20px;
        display: flex;
        flex-direction: row;
    }

    .data {
        margin-left: 20px;
        margin-top: 20px;
        margin-right: 10px;
    }
</style>
